const {exec, escape} = require('../db/mysql');
const {genPassword} = require('../utils/cryp');

const checkRegister = (username) => {
    username = escape(username);

    const sql = `
        select * from t_user where username=${username}
    `;
    return exec(sql).then(rows => {
        return rows[0] || {}
    })
};

const register = (realname,username,password) => {
    realname = escape(realname);
    username = escape(username);

    password = genPassword(password);
    password = escape(password);

    const sql = `
        insert into t_user (username,\`password\`,realname) values (${username},${password},${realname})
    `;
    return exec(sql).then(rows => {
        return rows[0] || {}
    })
};


module.exports = {
    checkRegister,
    register
};